/*
 * Copyright (c) 2018, apexes.net. All rights reserved.
 *
 *         http://www.apexes.net
 *
 */
package net.apexes.demo.codegen.enume.support;

import com.google.gson.JsonDeserializer;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializer;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 *
 * @author hedyn
 */
public class GsonJavaTimeSupport {

    private GsonJavaTimeSupport() {}

    public static JsonSerializer<LocalDate> createLocalDateJsonSerializer() {
        return (src, typeOfSrc, context) -> new JsonPrimitive(src.format(DateTimeFormatter.ISO_DATE));
    }

    public static JsonDeserializer<LocalDate> createLocalDateJsonDeserializer() {
        return (json, type, context) -> LocalDate.parse(json.getAsJsonPrimitive().getAsString(), DateTimeFormatter.ISO_DATE);
    }

    public static JsonSerializer<LocalDateTime> createLocalDateTimeJsonSerializer() {
        return createLocalDateTimeJsonSerializer(DateTimeFormatter.ISO_DATE_TIME);
    }

    public static JsonSerializer<LocalDateTime> createLocalDateTimeJsonSerializer(DateTimeFormatter dateTimeFormatter) {
        return (src, typeOfSrc, context) -> new JsonPrimitive(src.format(dateTimeFormatter));
    }

    public static JsonDeserializer<LocalDateTime> createLocalDateTimeJsonDeserializer() {
        return createLocalDateTimeJsonDeserializer(DateTimeFormatter.ISO_DATE_TIME);
    }

    public static JsonDeserializer<LocalDateTime> createLocalDateTimeJsonDeserializer(DateTimeFormatter dateTimeFormatter) {
        return (json, type, context) -> LocalDateTime.parse(json.getAsJsonPrimitive().getAsString(), dateTimeFormatter);
    }

}
